package com.test.mchelepesa.activity_fragment_rice.info_rice;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseInfoActivity_rice;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseApplication_rice;
import com.test.mchelepesa.constant_rice.Constant_rice;
import com.test.mchelepesa.dialog_rice.PhotoUploadDialog_rice;
import com.test.mchelepesa.util_rice.PermissionUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.util_rice.imageUtil_rice.ImageConvert_rice;
import com.test.mchelepesa.view_rice.widget_rice.TitleView_rice;

import java.io.File;
import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class OcrInfoActivity_rice extends BaseInfoActivity_rice {

    private View idCardView,passportView,drivingLicenseView,educationView;
    private ImageView idCardIv,passportIv,drivingLicenseIv, educationIv;
    private TextView idCardTv,passportTv,drivingLicenseTv,educationTv;
    private View idCardIcon,passportIcon,drivingLicenseIcon, educationIcon;
    private TextView typeTv;
    private TextView frontTitleTv;
    private TextView frontDescTv,backDescTv;
    private ImageView frontIv,backIv;
    private View backView;

    private View frontReuploadView,backReuploadView;

    private String image1Url, image2Url;

    private String CT_IdCard,CT_Passport,CT_DrvingLicense,CT_Education;
    private static final String WD_IdCard="IDCARD",WD_Passport="PASSPORT",WD_DrvingLicense="DRIVING",WD_EDUCATION = "EDUCATION";
    private String selectType = CT_IdCard;
    private String frontImgPath = "",backImgPath = "";
    private Uri frontImgUri,backImgUri;
    private Bitmap frontImgBm,backImgBm;
    private boolean uploadImgFlag = true;
    private Runnable runnableWithCameraPermission = null;

    private final int Color_Select = Color.parseColor("#FFFC724D");
    private final int Color_Unselect = Color.parseColor("#FFB1B5B9");

    private boolean deleteData = true;

    private final ActivityResultLauncher<Intent> startCameraForFrontArl = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK){
                    if (frontImgUri == null || TextUtils.isEmpty(frontImgPath)){
                        image1Url = "";
                        Toast.makeText(this,getString(R.string.select_img_fail_tips),Toast.LENGTH_SHORT).show();
                        return;
                    }
                    new Thread(() -> ImageConvert_rice.startConvertImage(
                            new File(frontImgPath),
                            (localPath, thumpImg) -> {
                                frontImgBm = thumpImg;
                                uploadImgFlag = true;
                                HttpManager_rice.getInstance().uploadImage(localPath);
                            }
                    )).start();
                }else Toast.makeText(this,getString(R.string.select_img_fail_tips),Toast.LENGTH_SHORT).show();
            }
    );

    private final ActivityResultLauncher<Intent> startCameraForBackArl = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK){
                    if (backImgUri == null || TextUtils.isEmpty(backImgPath)){
                        image2Url = "";
                        Toast.makeText(this,getString(R.string.select_img_fail_tips),Toast.LENGTH_SHORT).show();
                        return;
                    }
                    new Thread(() -> ImageConvert_rice.startConvertImage(
                            new File(backImgPath),
                            (localPath, thumpImg) -> {
                                backImgBm = thumpImg;
                                uploadImgFlag = false;
                                HttpManager_rice.getInstance().uploadImage(localPath);
                            }
                    )).start();
                }else Toast.makeText(this,getString(R.string.select_img_fail_tips),Toast.LENGTH_SHORT).show();
            }
    );

    private final ActivityResultLauncher<PickVisualMediaRequest> selectImgFromAlbumForFrontArl = registerForActivityResult(
            new ActivityResultContracts.PickVisualMedia(),
            uri -> {
                frontImgUri = uri;
                frontImgPath = ImageConvert_rice.getBitmapFilePathFromURI(this,frontImgUri);
                if (frontImgUri == null || TextUtils.isEmpty(frontImgPath)){
                    image1Url = "";
                    Toast.makeText(this,getString(R.string.photo_upload_again_tip),Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(() -> ImageConvert_rice.startConvertImage(
                        new File(frontImgPath),
                        (localPath, thumpImg) -> {
                            frontImgBm = thumpImg;
                            uploadImgFlag = true;
                            HttpManager_rice.getInstance().uploadImage(localPath);
                        }
                )).start();
            }
    );

    private final ActivityResultLauncher<PickVisualMediaRequest> selectImgFromAlbumForBackArl = registerForActivityResult(
            new ActivityResultContracts.PickVisualMedia(),
            uri -> {
                backImgUri = uri;
                backImgPath = ImageConvert_rice.getBitmapFilePathFromURI(this,backImgUri);
                if (backImgUri == null || TextUtils.isEmpty(backImgPath)){
                    image2Url = "";
                    Toast.makeText(this,getString(R.string.photo_upload_again_tip),Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(() -> ImageConvert_rice.startConvertImage(
                        new File(backImgPath),
                        (localPath, thumpImg) -> {
                            backImgBm = thumpImg;
                            uploadImgFlag = false;
                            HttpManager_rice.getInstance().uploadImage(localPath);
                        }
                )).start();
            }
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr_info_rice);

        TitleView_rice titleView = findViewById(R.id.ocr_title);
        titleView.getBackIb().setOnClickListener(v -> onBackPressed());

        initLicense(
                findViewById(R.id.ocr_view_license),
                findViewById(R.id.ocr_license_logo),
                findViewById(R.id.ocr_license_company_name)
        );

        idCardView = findViewById(R.id.ocr_view_type_id_card);
        passportView = findViewById(R.id.ocr_view_type_passport);
        drivingLicenseView = findViewById(R.id.ocr_view_type_driving_license);
        educationView = findViewById(R.id.ocr_view_type_education);
        idCardIv = findViewById(R.id.ocr_iv_type_id_card);
        passportIv = findViewById(R.id.ocr_iv_type_passport);
        drivingLicenseIv = findViewById(R.id.ocr_iv_type_driving_license);
        educationIv = findViewById(R.id.ocr_iv_type_education);
        idCardTv = findViewById(R.id.ocr_tv_type_id_card);
        passportTv = findViewById(R.id.ocr_tv_type_passport);
        drivingLicenseTv = findViewById(R.id.ocr_tv_type_driving_license);
        educationTv = findViewById(R.id.ocr_tv_type_education);
        idCardIcon = findViewById(R.id.ocr_icon_type_id_card);
        passportIcon = findViewById(R.id.ocr_icon_type_passport);
        drivingLicenseIcon = findViewById(R.id.ocr_icon_type_driving_license);
        educationIcon = findViewById(R.id.ocr_icon_type_education);
        typeTv = findViewById(R.id.ocr_tv_type);
        frontTitleTv = findViewById(R.id.ocr_tv_front_title);
        frontDescTv = findViewById(R.id.ocr_tv_front_desc);
        backDescTv = findViewById(R.id.ocr_tv_back_desc);
        frontIv = findViewById(R.id.ocr_iv_front);
        backIv = findViewById(R.id.ocr_iv_back);
        backView = findViewById(R.id.ocr_view_back);

        frontReuploadView = findViewById(R.id.ocr_view_front_reupload);
        backReuploadView = findViewById(R.id.ocr_view_back_reupload);

        View deleteDataView = findViewById(R.id.ocr_view_delete_data);
        ImageView deleteDataIv = findViewById(R.id.ocr_iv_delete_data);

        deleteDataView.setOnClickListener(v -> {
            deleteData = !deleteData;
            if (deleteData){
                deleteDataView.setBackgroundResource(R.drawable.ic_border_orange_rice_1dp_round_corner_white_10dp);
                deleteDataIv.setBackgroundResource(R.drawable.icon_select);
            }else {
                deleteDataView.setBackgroundResource(R.drawable.bg_white_round_corner_10);
                deleteDataIv.setBackgroundResource(R.drawable.icon_unselect);
            }
        });
        
        frontIv.setOnClickListener(v -> {

            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_IMAGE_FRONT_CLICK");

            new PhotoUploadDialog_rice(this,
                    () -> runnWithCameraPermission(() -> {
                        Intent intent = new Intent();
                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        String imageFilePath = imageFilePath();
                        File imageFile = new File(imageFilePath);
                        frontImgPath = imageFilePath;
                        String providerAuthority = BaseApplication_rice.gApplication_rice().getPackageName() + ".file_provider";
                        frontImgUri = FileProvider.getUriForFile(BaseApplication_rice.gApplication_rice(), providerAuthority, imageFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, frontImgUri);
                        startCameraForFrontArl.launch(intent);
                    }),
                    () -> selectImgFromAlbumForFrontArl.launch(
                            new PickVisualMediaRequest.Builder()
                                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                                    .build()
                    )
            ).show();

        });

        frontReuploadView.setOnClickListener(view -> {

            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_REUPLOAD_FRONT_CLICK");

            new PhotoUploadDialog_rice(this,
                    () -> runnWithCameraPermission(() -> {
                        Intent intent = new Intent();
                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        String imageFilePath = imageFilePath();
                        File imageFile = new File(imageFilePath);
                        frontImgPath = imageFilePath;
                        String providerAuthority = BaseApplication_rice.gApplication_rice().getPackageName() + ".file_provider";
                        frontImgUri = FileProvider.getUriForFile(BaseApplication_rice.gApplication_rice(), providerAuthority, imageFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, frontImgUri);
                        startCameraForFrontArl.launch(intent);
                    }),
                    () -> selectImgFromAlbumForFrontArl.launch(
                            new PickVisualMediaRequest.Builder()
                                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                                    .build()
                    )
            ).show();

        });

        backIv.setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_IMAGE_BACK_CLICK");

            new PhotoUploadDialog_rice(this,
                    () -> runnWithCameraPermission(() -> {
                        Intent intent = new Intent();
                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        String imageFilePath = imageFilePath();
                        File imageFile = new File(imageFilePath);
                        backImgPath = imageFilePath;
                        String providerAuthority = BaseApplication_rice.gApplication_rice().getPackageName() + ".file_provider";
                        backImgUri = FileProvider.getUriForFile(BaseApplication_rice.gApplication_rice(), providerAuthority, imageFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, backImgUri);
                        startCameraForBackArl.launch(intent);
                    }),
                    () -> selectImgFromAlbumForBackArl.launch(
                            new PickVisualMediaRequest.Builder()
                                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                                    .build()
                    )
            ).show();
        });

        backReuploadView.setOnClickListener(view -> {

            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_REUPLOAD_BACK_CLICK");

            new PhotoUploadDialog_rice(this,
                    () -> runnWithCameraPermission(() -> {
                        Intent intent = new Intent();
                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        String imageFilePath = imageFilePath();
                        File imageFile = new File(imageFilePath);
                        backImgPath = imageFilePath;
                        String providerAuthority = BaseApplication_rice.gApplication_rice().getPackageName() + ".file_provider";
                        backImgUri = FileProvider.getUriForFile(BaseApplication_rice.gApplication_rice(), providerAuthority, imageFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, backImgUri);
                        startCameraForBackArl.launch(intent);
                    }),
                    () -> selectImgFromAlbumForBackArl.launch(
                            new PickVisualMediaRequest.Builder()
                                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                                    .build()
                    )
            ).show();

        });

        findViewById(R.id.ocr_btn_next).setOnClickListener(v -> {

            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload("OCR_NEXT_CLICK");

            if (TextUtils.isEmpty(image1Url)||(TextUtils.isEmpty(image2Url)&&!selectType.equalsIgnoreCase(CT_Passport))){
                showToast_rice(getString(R.string.upload_ocr_tip));
                return;
            }

            HttpManager_rice.getInstance().uploadOcrInfo(getTypeWd(),image1Url,selectType.equalsIgnoreCase(CT_Passport) ? "":image2Url,deleteData);

        });

        CT_IdCard = getString(R.string.identity_ocr_type_id_card);
        CT_Passport = getString(R.string.identity_ocr_type_passport);
        CT_DrvingLicense = getString(R.string.identity_ocr_type_driving_license);
        CT_Education = getString(R.string.identity_ocr_type_education);

        selectType = CT_IdCard;

        idCardView.setOnClickListener(v -> {
            if (CT_IdCard.equalsIgnoreCase(selectType)) return;
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_TYPE_ID_CARD_SELECT");
            initTypeIdCard();
        });

        passportView.setOnClickListener(v -> {
            if (CT_Passport.equalsIgnoreCase(selectType)) return;
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_TYPE_PASSPORT_SELECT");
            initTypePassport();
        });

        drivingLicenseView.setOnClickListener(v -> {
            if (CT_DrvingLicense.equalsIgnoreCase(selectType)) return;
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_TYPE_DRIVING_LICENSE_SELECT");
            initTypeDrvingLicense();
        });

        educationView.setOnClickListener(v -> {
            if (CT_Education.equalsIgnoreCase(selectType)) return;
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_TYPE_EDUCATION_CARD_SELECT");
            initTypeEducation();
        });
    }

    public void onPhotoUploadResponse(String imageUrl){

        if (uploadImgFlag){
            runOnUiThread(() -> {
                if (TextUtils.isEmpty(imageUrl) || frontImgBm == null){
                    Toast.makeText(this,getString(R.string.upload_ocr_tip),Toast.LENGTH_SHORT).show();
                    return;
                }
                image1Url = imageUrl;
                frontIv.setImageBitmap(frontImgBm);
                frontReuploadView.setVisibility(VISIBLE);
            });
        }

        else {
            runOnUiThread(() -> {
                if (TextUtils.isEmpty(imageUrl) || backImgBm == null){
                    Toast.makeText(this,getString(R.string.upload_ocr_tip),Toast.LENGTH_SHORT).show();
                    return;
                }
                image2Url = imageUrl;
                backIv.setImageBitmap(backImgBm);
                backReuploadView.setVisibility(VISIBLE);
            });
        }

    }

    @SuppressLint("SetTextI18n")
    private void initTypeIdCard(){
        selectType = CT_IdCard;

        idCardView.setBackgroundResource(R.drawable.bg_ocr_select);
        passportView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        drivingLicenseView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        educationView.setBackgroundResource(R.drawable.bg_ocr_unselect);

        idCardIv.setBackgroundResource(R.drawable.icon_id_card_true);
        passportIv.setBackgroundResource(R.drawable.icon_passport_false);
        drivingLicenseIv.setBackgroundResource(R.drawable.icon_driving_license_false);
        educationIv.setBackgroundResource(R.drawable.icon_education_false);

        idCardTv.setTextColor(Color_Select);
        passportTv.setTextColor(Color_Unselect);
        drivingLicenseTv.setTextColor(Color_Unselect);
        educationTv.setTextColor(Color_Unselect);

        idCardIcon.setBackgroundResource(R.drawable.icon_ocr_select);
        passportIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        drivingLicenseIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        educationIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);

        typeTv.setText(getString(R.string.identity_ocr_type_id_card));

        frontTitleTv.setText(getString(R.string.identity_ocr_front_title));
        frontDescTv.setText(getString(R.string.identity_ocr_id_card_front_desc));
        backDescTv.setText(getString(R.string.identity_ocr_id_card_back_desc));

        frontIv.setImageResource(R.drawable.img_ocr_id_card_front);
        frontReuploadView.setVisibility(GONE);

        backIv.setImageResource(R.drawable.img_ocr_id_card_back);
        backReuploadView.setVisibility(GONE);

        backView.setVisibility(VISIBLE);

    }

    @SuppressLint("SetTextI18n")
    private void initTypePassport(){
        selectType = CT_Passport;

        idCardView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        passportView.setBackgroundResource(R.drawable.bg_ocr_select);
        drivingLicenseView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        educationView.setBackgroundResource(R.drawable.bg_ocr_unselect);

        idCardIv.setBackgroundResource(R.drawable.icon_id_card_false);
        passportIv.setBackgroundResource(R.drawable.icon_passport_true);
        drivingLicenseIv.setBackgroundResource(R.drawable.icon_driving_license_false);
        educationIv.setBackgroundResource(R.drawable.icon_education_false);

        idCardTv.setTextColor(Color_Unselect);
        passportTv.setTextColor(Color_Select);
        drivingLicenseTv.setTextColor(Color_Unselect);
        educationTv.setTextColor(Color_Unselect);

        idCardIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        passportIcon.setBackgroundResource(R.drawable.icon_ocr_select);
        drivingLicenseIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        educationIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);

        typeTv.setText(getString(R.string.identity_ocr_type_passport));

        frontTitleTv.setText(getString(R.string.identity_ocr_passport_title));
        frontDescTv.setText(getString(R.string.identity_ocr_passport_desc));

        frontIv.setImageResource(R.drawable.img_ocr_passport);
        frontReuploadView.setVisibility(GONE);

        backView.setVisibility(GONE);
        
    }

    @SuppressLint("SetTextI18n")
    private void initTypeDrvingLicense(){
        selectType = CT_DrvingLicense;

        idCardView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        passportView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        drivingLicenseView.setBackgroundResource(R.drawable.bg_ocr_select);
        educationView.setBackgroundResource(R.drawable.bg_ocr_unselect);

        idCardIv.setBackgroundResource(R.drawable.icon_id_card_false);
        passportIv.setBackgroundResource(R.drawable.icon_passport_false);
        drivingLicenseIv.setBackgroundResource(R.drawable.icon_driving_license_true);
        educationIv.setBackgroundResource(R.drawable.icon_education_false);

        idCardTv.setTextColor(Color_Unselect);
        passportTv.setTextColor(Color_Unselect);
        drivingLicenseTv.setTextColor(Color_Select);
        educationTv.setTextColor(Color_Unselect);

        idCardIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        passportIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        drivingLicenseIcon.setBackgroundResource(R.drawable.icon_ocr_select);
        educationIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);

        typeTv.setText(getString(R.string.identity_ocr_type_driving_license));

        frontTitleTv.setText(getString(R.string.identity_ocr_front_title));
        frontDescTv.setText(getString(R.string.identity_ocr_driving_license_front_desc));
        backDescTv.setText(getString(R.string.identity_ocr_driving_license_back_desc));

        frontIv.setImageResource(R.drawable.img_ocr_driving_license_front);
        frontReuploadView.setVisibility(GONE);

        backIv.setImageResource(R.drawable.img_ocr_driving_license_back);
        backReuploadView.setVisibility(GONE);

        backView.setVisibility(VISIBLE);

    }

    @SuppressLint("SetTextI18n")
    private void initTypeEducation(){
        selectType = CT_Education;

        idCardView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        passportView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        drivingLicenseView.setBackgroundResource(R.drawable.bg_ocr_unselect);
        educationView.setBackgroundResource(R.drawable.bg_ocr_select);

        idCardIv.setBackgroundResource(R.drawable.icon_id_card_false);
        passportIv.setBackgroundResource(R.drawable.icon_passport_false);
        drivingLicenseIv.setBackgroundResource(R.drawable.icon_driving_license_false);
        educationIv.setBackgroundResource(R.drawable.icon_education_true);

        idCardTv.setTextColor(Color_Unselect);
        passportTv.setTextColor(Color_Unselect);
        drivingLicenseTv.setTextColor(Color_Unselect);
        educationTv.setTextColor(Color_Select);

        idCardIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        passportIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        drivingLicenseIcon.setBackgroundResource(R.drawable.icon_ocr_unselect);
        educationIcon.setBackgroundResource(R.drawable.icon_ocr_select);

        typeTv.setText(getString(R.string.identity_ocr_type_education));

        frontTitleTv.setText(getString(R.string.identity_ocr_front_title));
        frontDescTv.setText(getString(R.string.identity_ocr_education_front_desc));
        backDescTv.setText(getString(R.string.identity_ocr_education_back_desc));

        frontIv.setImageResource(R.drawable.img_ocr_education_front);
        frontReuploadView.setVisibility(GONE);

        backIv.setImageResource(R.drawable.img_ocr_education_back);
        backReuploadView.setVisibility(GONE);

        backView.setVisibility(VISIBLE);

    }

    private String getTypeWd(){
        if (TextUtils.isEmpty(selectType)) return "";

        if (CT_IdCard.equalsIgnoreCase(selectType)) return WD_IdCard;
        else if (CT_Passport.equalsIgnoreCase(selectType)) return WD_Passport;
        else if (CT_DrvingLicense.equalsIgnoreCase(selectType)) return WD_DrvingLicense;
        else if (CT_Education.equalsIgnoreCase(selectType)) return WD_EDUCATION;
        return "";
    }

    private void runnWithCameraPermission(Runnable runnable){
        if (PermissionUtil_rice.checkLaunchPermissions(this, Manifest.permission.CAMERA)){
            if (runnable!=null) runnable.run();
        }else {
            this.runnableWithCameraPermission = runnable;
            EasyPermissions.requestPermissions(this,getString(R.string.request_camera_permission),PermissionUtil_rice.REQ_CODE_PERMISSIONS_CAMERA,Manifest.permission.CAMERA);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PermissionUtil_rice.REQ_CODE_PERMISSIONS_CAMERA && runnableWithCameraPermission!=null){
            HttpManager_rice.getInstance().saveUserBuriedPoint("OCR_CAMERA_PERMISSION_AGREE_CLICK");
            runnableWithCameraPermission.run();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this,perms) && requestCode == PermissionUtil_rice.REQ_CODE_PERMISSIONS_CAMERA){
            new AppSettingsDialog.Builder(this).build().show();
        }
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (requestCode == PermissionUtil_rice.REQ_CODE_PERMISSIONS_CAMERA){
            EasyPermissions.requestPermissions(this,getString(R.string.request_camera_permission),PermissionUtil_rice.REQ_CODE_PERMISSIONS_CAMERA,Manifest.permission.CAMERA);
        }
    }

    public String imageFilePath() {
        String pictureId = System.currentTimeMillis() + "";
        String imgDir = sandboxFolderPath();
        return (imgDir + File.separator + pictureId + ".jpg");
    }

    public String sandboxFolderPath() {
        String inAppFolder = getFilesDir().getAbsolutePath();
        inAppFolder += File.separator;
        inAppFolder += Constant_rice.PRODUCT;
        inAppFolder += "_PICS";

        File folderFile = new File(inAppFolder);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }

        return inAppFolder;
    }


}